#!/usr/bin/env python
# -*- encoding: utf-8 -*-
#
#
#
import socket

class BaseClient(object):
    def __init__(self,host,port):
        self.host = host
        self.port = port
class TCPClient(BaseClient):
    socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
class UDPClient(BaseClient):
    socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

class HostRequest(object):
    def __init__(self,host, port=80, mode='TCP'):
        self.host = host
        self.port = int(port)
        self.mode = mode
        if self.mode=='TCP':
            self.socket = TCPClient()
        else:  #UDP
            self.socket = UDPClient()

        self.socket.connect((self.host,self.port)))
        self.socket.close()
        del self.socket

    def send(self,msg):
        if self.mode=='TCP':
            self.socket.send(msg)
        else:
            self.socket.sendto(msg)

    def close(self):
        self.socket.close()
        del self.socket

